package com.growthgate.bootcommon.util;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

/**
 * 数组工具类
 *
 * @author GrowthGate
 * @date 2019/11/1
 */
public class ArrayUtils extends org.apache.commons.lang3.ArrayUtils {

    /**
     * 将对象转化为object数组
     *
     * @param obj 对象
     * @return object数组
     */
    @Nullable
    @Contract("null -> null")
    public static Object[] toObject(@Nullable Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj.getClass().isArray()) {
            if (obj instanceof boolean[]) {
                return toObject((boolean[]) obj);
            } else if (obj instanceof byte[]) {
                return toObject((byte[]) obj);
            } else if (obj instanceof char[]) {
                return toObject((char[]) obj);
            } else if (obj instanceof short[]) {
                return toObject((short[]) obj);
            } else if (obj instanceof int[]) {
                return toObject((int[]) obj);
            } else if (obj instanceof long[]) {
                return toObject((long[]) obj);
            } else if (obj instanceof float[]) {
                return toObject((float[]) obj);
            } else if (obj instanceof double[]) {
                return toObject((double[]) obj);
            } else {
                return (Object[]) obj;
            }
        } else if (obj instanceof Collection<?>) {
            return ((Collection<?>) obj).toArray();
        } else {
            return new Object[]{obj};
        }
    }

}